
<template>
  <el-button type="danger" :icon="Plus" @click="onSubmit()">新增</el-button>
   <el-form :inline="true" :model="adddata" class="demo-form-inline">
       <el-form-item label="耳号">
         <el-input
         v-model="adddata.pigear"
           placeholder="请输耳号"
           clearable
         />
       </el-form-item>
       <el-form-item>
         <el-button type="danger" @click="gettableData()">搜索</el-button>
       </el-form-item>
     </el-form>
     <el-table :data="tableData" style="width: 100%">
       <el-table-column prop="pigId" label="序号" />
       <el-table-column prop="pigNo" label="单据号" />
       <el-table-column prop="pigTime" label="期初日期" >
        <template #default="scope">
        {{scope.row.pigTime.substr(0,10).replace(" ","T")}}
        </template>
       </el-table-column>
       <el-table-column prop="pigear" label="耳号" />
       <el-table-column prop="bedroomName" label="栋舍名称" />
       <el-table-column prop="pigClass" label="阶段" />
       <el-table-column prop="pigweight" label="重量(公斤)" />


       <el-table-column prop="" label="操作">
        <template #default="scope">
           <el-button type="primary" @click="handleEdit(scope.row)">删除</el-button>
           <el-button type="danger" @click="handleDelete(scope.row)">修改</el-button>
         </template>
       </el-table-column>

     </el-table>
      <!-- 分页 -->
      <div>
       <el-pagination
         v-model:current-page="adddata.pageIndex"
         v-model:page-size="adddata.pageSize"
         :page-sizes="[3, 5, 8, 10]"
         :background="true"
         layout="total, sizes, prev, pager, next, jumper"
         :total="adddata.totalCount"
         @size-change="handleSizeChange"
         @current-change="handleCurrentChange"
       />
     </div>
</template>

<script lang="ts" setup>
import axios from 'axios';
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { useRoute, useRouter } from 'vue-router'
import { Delete, Edit, Search, Share, Upload } from '@element-plus/icons-vue'
import type { ComponentSize, FormInstance, FormRules } from 'element-plus'
import { Plus } from '@element-plus/icons-vue'
import type { UploadProps, UploadUserFile } from 'element-plus'

const apiUrl = import.meta.env.VITE_APP_API_URL;
const apiBedinUrl = import.meta.env.VITE_APPRead_API_URL;

const route = useRoute()
const router = useRouter()
const date = new Date();
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hour = date.getHours();
const minute = date.getMinutes();

const random = Math.floor(Math.random() * 1000);
const pigNo = year + '' + month + '' + day + '' + hour + '' + minute + '' + '' + random;
  const adddata = reactive({

    "pigNo":pigNo,
    pigear:'',
     pageIndex: 1,
     pageSize: 3,
     totalCount: 0,
     pageCount: 0,
   })

   const tableData = ref([])
   const gettableData = () => {
     axios.post(apiUrl+'/api/Pig/ShowPigBabys', {
      pigear:adddata.pigear,
       pageIndex: adddata.pageIndex,
       pageSize: adddata.pageSize
     }).then(res => {
       tableData.value = res.data.data.list
       adddata.pageCount = res.data.data.pageCount;
       adddata.totalCount = res.data.data.totalCount;
     })
   }
   //分页
   const handleSizeChange = (val: number) => {
     console.log(`${val} items per page`)
     adddata.pageSize=val
     gettableData()
   }
   const handleCurrentChange = (val: number) => {
     console.log(`current page: ${val}`)
     adddata.pageIndex=val
     gettableData()
  }


  // //删除
  // const handleEdit = (ablactationId: any) => {
  //   ElMessageBox.confirm(
  //     '确认要删除吗?',
  //     'Warning',
  //     {
  //       confirmButtonText: '确认',
  //       cancelButtonText: '取消',
  //       type: 'warning',
  //     }
  //   )
  //     .then(() => {
  //       axios.put(apiUrl+'/api/Ablactations/DeleteAblactation', {
  //         ablactationId: ablactationId
  //       })
  //         .then(res => {
  //           if (res.data.code == 0) {
  //             ElMessage.success('删除成功');
  //             gettableData()
  //           }
  //           else {
  //             ElMessage.error('删除失败');
  //             return;
  //           }
  //         })
  //     })
  //     .catch(() => {
  //       ElMessage({
  //         type: 'info',
  //         message: '取消删除',
  //       })
  //     })
  // }
  // //查看母猪断奶修改
  // const handleDelete = (ablactationId: number) => {
  //   router.push({
  //     path: '/',
  //     query: {
  //       ablactationId: ablactationId
  //     }
  //   })
  // }
  const onSubmit = () => {
    router.push({
      path: '/AddPig',
    })
  }

   onMounted(() => {
     gettableData()
   })
   </script>
